<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%" max-height="600">
      <el-table-column prop="orderBy" label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.row.orderBy || 10 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="用户">
        <template slot-scope="scope">
          {{ scope.row.sysUser | showUserInfo }}
        </template>
      </el-table-column>
      <el-table-column prop="auditStatus" label="状态">
        <template slot-scope="scope">
          <span v-show="scope.row.auditStatus == 1">待审核</span>
          <span v-show="scope.row.auditStatus == 2">审核通过</span>
          <span v-show="scope.row.auditStatus == 3">不通过</span>
        </template>
      </el-table-column>
      <el-table-column prop="des" label="申请描述">
        <template slot-scope="scope">{{ scope.row.des || '--' }}</template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="280">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button
              type="primary"
              size="mini"
              @click="handleEditTeacher(row)"
            >
              编辑
            </el-button>
            <el-button
              v-if="row.auditStatus == 1"
              type="primary"
              size="mini"
              @click="handleAudit(row)"
            >
              审核
            </el-button>
            <el-button
              v-if="row.sysUser.del == 2"
              type="primary"
              size="mini"
              @click="handleBuild(row)"
            >
              授权共建
            </el-button>
            <el-button type="primary" size="mini" @click="handleSort(row)">
              排序
            </el-button>
            <el-button
              :disabled="row.sysUser.id == row.courseOriginator"
              type="primary"
              size="mini"
              @click="handleUpdateOriginator(row)"
            >
              设为创建人
            </el-button>
            <el-button
              :disabled="row.sysUser.id == row.courseOriginator"
              type="danger"
              size="mini"
              @click="handleDelete(row)"
            >
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
    <AddTeacher @e-userData="handleAdd" />
    <EditTeacher />
    <Audit />
    <Build />
    <Sort />
    <UserSelect
      ref="userSelect"
      :initialValue="initialValue"
      @e-userData="handleAdd"
    />
  </div>
</template>
<script>
import {
  sysCourseTeamQuery,
  sysCourseTeamDelete,
  sysCourseTeamUpdateOriginator,
} from '@/api/admin/courseLibrary/sysCourseTeam.js'
import courseTeamAdd from '@/mixins/courseTeamAdd'
import Bus from '@/utils/bus.js'
export default {
  mixins: [courseTeamAdd],
  components: {
    AddTeacher: () => import('./AddTeacher.vue'),
    EditTeacher: () => import('./EditTeacher.vue'),
    Audit: () => import('./Audit.vue'),
    Build: () => import('./Build.vue'),
    Sort: () => import('./Sort.vue'),
    UserSelect: () => import('@/components/userSelect'),
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        courseId: this.$store.getters.courseId,
      },
      tableData: [],
      pageObj: {},
      initialValue: {
        userId: [],
      },
    }
  },
  mounted() {
    this.getList()
    this.getAllList()
    Bus.$on('refreshCourseTeam', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    Bus.$off('refreshCourseTeam')
  },
  methods: {
    getList() {
      sysCourseTeamQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.tableData = this.tableData.filter((v) =>
          v.hasOwnProperty('sysUser'),
        )
        this.pageObj = res.data
      })
    },
    //查询所有已选教师
    getAllList() {
      let data = Object.assign({}, this.listQuery)
      data.limit = 999
      sysCourseTeamQuery(data).then((res) => {
        let { data } = res.data
        this.initialValue.userId = data.map((v) => v.userId)
      })
    },
    //编辑教师信息
    handleEditTeacher(row) {
      Bus.$emit('showDialogCourseTeamEditTeacher', row)
    },
    //审核
    handleAudit(row) {
      Bus.$emit('showDialogCourseTeamAudit', row)
    },
    //授权共建
    handleBuild(row) {
      Bus.$emit('showDialogCourseBuildAudit', row)
    },
    //排序
    handleSort(row) {
      Bus.$emit('showDialogCourseTeamSort', row)
    },
    //修改课程创建人
    handleUpdateOriginator(row) {
      let { courseId, sysUser } = row
      sysCourseTeamUpdateOriginator({
        courseId,
        courseOriginator: sysUser.id,
      }).then((res) => {
        if (!res || res.data.code != 0) return
        this.getList()
      })
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      sysCourseTeamDelete(row.id).then((res) => {
        this.getList()
        this.getAllList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
  },
}
</script>